package gov.fnal.xpathsearch.rest;

import gov.fnal.xpath2ldap.XPathSearchOptions;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import java.util.ArrayList;

@XmlRootElement(name = "xpathsearch")
@XmlAccessorType(XmlAccessType.FIELD)
public class XPathQuery
{
   private static String[] str_arr = new String[]{};
   public static final String TRUE = "true";
   public static final String COLON = ":";
   public static final String SPACE = " ";
   public static final String LINEFEED = "\n";
   public static final String MINUS = "-";

   @XmlElement
   private String host_url;
   private String ldap_base;
   private String use_ldap_aux;
   private String output_ldap_dn;
   private String output_all_ldap_attributes;
   private String action;
   private String expr;

   public XPathQuery()
   {
   }

   public String getHostUrl()
   {
      return host_url;
   }

   public String getLDAPBase()
   {
      return ldap_base;
   }

   public String getUseLDAPAux()
   {
      return use_ldap_aux;
   }

    public String getOutputLDAPDN()
   {
      return output_ldap_dn;
   }

    public String getOutputAllLDAPAttributes()
   {
      return output_all_ldap_attributes;
   }

    public String getAction()
   {
      return action;
   }

   public String getExpr()
   {
      return expr;
   }

   public void setHostUrl(String host_url)
   {
      this.host_url=host_url;
   }

   public void setLDAPBase(String ldap_base)
   {
	   this.ldap_base=ldap_base;
   }

   public void setUseLDAPAux(String use_ldap_aux)
   {
	   this.use_ldap_aux=use_ldap_aux;
   }

    public void setOutputLDAPDN(String output_ldap_dn)
   {
    	this.output_ldap_dn=output_ldap_dn;
   }

    public void setOutputAllLDAPAttributes(String output_all_ldap_attributes)
   {
    	this.output_all_ldap_attributes=output_all_ldap_attributes;
   }

    public void setAction(String action)
   {
    	this.action=action;
   }

   public void setExpr(String expr)
   {
	   this.expr=expr;
   }
   
   public String toString() {
       return  XPathSearchOptions.B + COLON + SPACE + getLDAPBase() + LINEFEED +
               XPathSearchOptions.USE_LDAP_AUX + COLON + SPACE + getUseLDAPAux() + LINEFEED +
               XPathSearchOptions.OUTPUT_LDAP_DN + COLON + SPACE + getOutputLDAPDN() + LINEFEED +
               XPathSearchOptions.OUTPUT_ALL_LDAP_ATTRIBUTES + COLON + SPACE + getOutputAllLDAPAttributes() + LINEFEED +
               XPathSearchOptions.ACTION + COLON + SPACE + getAction() + LINEFEED +
               getHostUrl() + LINEFEED +
               getExpr();
   }

   public String toCliParseable() {
       StringBuffer cli = new StringBuffer();
       if(host_url!=null) cli.append(SPACE).append(MINUS).append(XPathSearchOptions.H).append(SPACE).append(host_url);
       if(ldap_base!=null) cli.append(SPACE).append(MINUS).append(XPathSearchOptions.B).append(SPACE).append(ldap_base);
       if(TRUE.equals(use_ldap_aux)) cli.append(SPACE).append(MINUS).append(XPathSearchOptions.USE_LDAP_AUX);
       if(TRUE.equals(output_ldap_dn)) cli.append(SPACE).append(MINUS).append(XPathSearchOptions.OUTPUT_LDAP_DN);
       if(TRUE.equals(output_all_ldap_attributes)) cli.append(SPACE).append(MINUS).append(XPathSearchOptions.OUTPUT_ALL_LDAP_ATTRIBUTES);
       if(action!=null) cli.append(SPACE).append(MINUS).append(XPathSearchOptions.ACTION).append(SPACE).append(action);
       if(expr!=null) cli.append(SPACE).append(expr);
       return  cli.toString();
   }

   public String[] toArray() {
       ArrayList<String> arr = new ArrayList<String>();
       if(host_url!=null) {arr.add(MINUS+XPathSearchOptions.H); arr.add(host_url);}
       if(ldap_base!=null) {arr.add(MINUS+XPathSearchOptions.B); arr.add(ldap_base);}
       if(TRUE.equals(use_ldap_aux)) {arr.add(MINUS+XPathSearchOptions.USE_LDAP_AUX);}
       if(TRUE.equals(output_ldap_dn)) {arr.add(MINUS+XPathSearchOptions.OUTPUT_LDAP_DN);}
       if(TRUE.equals(output_all_ldap_attributes)) {arr.add(MINUS+XPathSearchOptions.OUTPUT_ALL_LDAP_ATTRIBUTES);}
       if(action!=null) {arr.add(MINUS+XPathSearchOptions.ACTION); arr.add(action);}
       if(expr!=null) {arr.add(expr);}
       arr.trimToSize();
       return arr.toArray(str_arr);
   }
}